package cn.book.bus.controller;



import cn.book.bus.domain.*;
import cn.book.bus.service.*;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.val;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 学生测评结果表1(BoEuStudentEavResult)表控制层
 *
 * @author makejava
 * @since 2024-01-11 16:09:38
 */
@RestController
@RequestMapping("boEuStudentEavResult")
public class BoEuStudentEavResultController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private BoEuStudentEavResultService boEuStudentEavResultService;


    @Resource
    private BoEuDbTeacherService boEuDbTeacherService;

    @Resource
    private OrgdepartmentService orgdepartmentService;
    @Resource
    private OrguserService orguserService;




    @Resource
    private ResultsService resultsService;

    @Resource
    private WaterClassService waterClassService;

    @Resource
    private PerclaService perclaService;

    @Resource
    private YiService yiService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param boEuStudentEavResult 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<BoEuStudentEavResult> page, BoEuStudentEavResult boEuStudentEavResult) {
        return success(this.boEuStudentEavResultService.page(page, new QueryWrapper<>(boEuStudentEavResult)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.boEuStudentEavResultService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param boEuStudentEavResult 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody BoEuStudentEavResult boEuStudentEavResult) {
        return success(this.boEuStudentEavResultService.save(boEuStudentEavResult));
    }

    /**
     * 修改数据
     *
     * @param boEuStudentEavResult 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody BoEuStudentEavResult boEuStudentEavResult) {
        return success(this.boEuStudentEavResultService.updateById(boEuStudentEavResult));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.boEuStudentEavResultService.removeByIds(idList));
    }


//    @GetMapping("list")
//    public void simpleRead1() {
//
//        final ArrayList<banji> arrayList = new ArrayList<>();
//        final List<BoEuDbTeacher> list = boEuDbTeacherService.list();
//
//
//
//
//
//
//        for (BoEuDbTeacher boEuDbTeacher : list) {
//            final banji banji = new banji();
//            banji.setId(boEuDbTeacher.getTeacherCode());
//            banji.setName(boEuDbTeacher.getTeacherName());
//
//
//
//            final String teacherCode = boEuDbTeacher.getTeacherCode();
//            final LambdaQueryWrapper<BoEuStudentEavResult> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(BoEuStudentEavResult::getTeacherCode, teacherCode);
//            final int count = boEuStudentEavResultService.count(wrapper);
//            banji.setTatol(String.valueOf(count));
//
//            final LambdaQueryWrapper<BoEuStudentEavResult> wrapper1 = new LambdaQueryWrapper<>();
//            wrapper1.eq(BoEuStudentEavResult::getTeacherCode, teacherCode).eq(BoEuStudentEavResult::getTaskStatus,"未评教");
//            final int count1 = boEuStudentEavResultService.count(wrapper1);
//            banji.setWeiping(String.valueOf(count1));
//
//
//            final LambdaQueryWrapper<BoEuStudentEavResult> wrapper2 = new LambdaQueryWrapper<>();
//            wrapper2.eq(BoEuStudentEavResult::getTeacherCode, teacherCode).eq(BoEuStudentEavResult::getTaskStatus,"已评教");
//            final int count2 = boEuStudentEavResultService.count(wrapper2);
//            banji.setYiping(String.valueOf(count2));
//
//            arrayList.add(banji);
//        }
//
//
//        String fileName2 = "C:\\Users\\DTRL002\\Desktop\\book_novels\\book\\src\\main\\java\\cn\\book\\bus\\controller" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
//        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//        // 如果这里想使用03 则 传入excelType参数即可
//        EasyExcel.write(fileName2, banji.class)
//                .sheet("模板")
//                .doWrite(() -> {
//                    // 分页查询数据
//                    return arrayList;
//                });
//
//    }
    @GetMapping("list")
    public void simpleRead1() {


        // 创建一个定长线程池，例如5个线程
        int threadCount = 10;
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);


        final ArrayList<banji> arrayList = new ArrayList<>();
        final List<BoEuDbTeacher> list = boEuDbTeacherService.list();

        // 数据分页大小，每个线程处理多少数据
        int batchSize = (list.size() + threadCount - 1) / threadCount;

        // 将大列表拆分成多个小列表，然后提交到线程池处理
        for (int i = 0; i < list.size(); i += batchSize) {
            final int fromIndex = i;
            final int toIndex = Math.min(fromIndex + batchSize, list.size());

            Runnable worker = () -> processBatch(list.subList(fromIndex, toIndex),arrayList);
            executorService.submit(worker);
        }

        // 关闭线程池，等待所有任务完成
        executorService.shutdown();
        try {
            // 阻塞直到所有任务都已完成
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("Interrupted while waiting for tasks to terminate.");
        }

        String fileName2 = "C:\\Users\\DTRL002\\Desktop\\book_novels\\book\\src\\main\\java\\cn\\book\\bus\\controller" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName2, banji.class)
                .sheet("模板")
                .doWrite(() -> {
                    // 分页查询数据
                    return arrayList;
                });
    }



    private  void processBatch(List<BoEuDbTeacher> batch,ArrayList<banji> arrayList) {
        for (BoEuDbTeacher boEuDbTeacher : batch) {
            final banji banji = new banji();
            banji.setId(boEuDbTeacher.getTeacherCode());
            banji.setName(boEuDbTeacher.getTeacherName());



            final String teacherCode = boEuDbTeacher.getTeacherCode();
            final LambdaQueryWrapper<BoEuStudentEavResult> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BoEuStudentEavResult::getTeacherCode, teacherCode);
            final int count = boEuStudentEavResultService.count(wrapper);
            banji.setTatol(String.valueOf(count));

            final LambdaQueryWrapper<BoEuStudentEavResult> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(BoEuStudentEavResult::getTeacherCode, teacherCode).eq(BoEuStudentEavResult::getTaskStatus,"未评教");
            final int count1 = boEuStudentEavResultService.count(wrapper1);
            banji.setWeiping(String.valueOf(count1));


            final LambdaQueryWrapper<BoEuStudentEavResult> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(BoEuStudentEavResult::getTeacherCode, teacherCode).eq(BoEuStudentEavResult::getTaskStatus,"已评教");
            final int count2 = boEuStudentEavResultService.count(wrapper2);
            banji.setYiping(String.valueOf(count2));

            arrayList.add(banji);
        }

    }





//    @GetMapping("banji")
//    public void simple() {
//
//        final ArrayList<Bclass> arrayList = new ArrayList<>();
//
//        //所有班级
//        final List<Orgdepartment> list = orgdepartmentService.list().subList(0,2);
//
//
//        for (Orgdepartment orgdepartment : list) {
//            System.out.println(orgdepartment.getDepartmentname());
//            final LambdaQueryWrapper<Orguser> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(Orguser::getDepartmentid, orgdepartment.getId());
//            //找学生
//            final List<Orguser> orguserList = orguserService.list(wrapper);
//
//            int count = 0;
//            int yiping = 0;
//            int weiping = 0;
//
//            for (Orguser orguser : orguserList) {
//                //总数
//                final LambdaQueryWrapper<BoEuStudentEavResult> countwrapper = new LambdaQueryWrapper<>();
//                countwrapper.eq(BoEuStudentEavResult::getStudentId, orguser.getUserid());
//                int i  = boEuStudentEavResultService.count(countwrapper);
//                count = count + i;
//                System.out.println("学生班级id==="+orgdepartment.getDepartmentname());
//                System.out.println(orguser.getUsername()+"==="+i);
//                //已评
//                final LambdaQueryWrapper<BoEuStudentEavResult> yipingwrapper = new LambdaQueryWrapper<>();
//                yipingwrapper.eq(BoEuStudentEavResult::getStudentId, orguser.getUserid()).eq(BoEuStudentEavResult::getTaskStatus,"已评教");
//                int j  = boEuStudentEavResultService.count(yipingwrapper);
//                yiping = yiping + j;
//
//                //未评
//                final LambdaQueryWrapper<BoEuStudentEavResult> weipingwrapper = new LambdaQueryWrapper<>();
//                weipingwrapper.eq(BoEuStudentEavResult::getStudentId, orguser.getUserid()).eq(BoEuStudentEavResult::getTaskStatus,"未评教");
//                int q  = boEuStudentEavResultService.count(weipingwrapper);
//                weiping = weiping + q;
//
//
//            }
//
//            final Bclass bclass = new Bclass();
//            bclass.setId(orgdepartment.getId());
//            bclass.setName(orgdepartment.getDepartmentname());
//            bclass.setTatol(String.valueOf(count));
//            bclass.setYiping(String.valueOf(yiping));
//            bclass.setWeiping(String.valueOf(weiping));
//            arrayList.add(bclass);
//
//
//            System.out.println("111111111111111111111111111111111111111111111111111"+bclass);
//            System.out.println("count====="+count);
////            orguserList.forEach(orguser ->
////                        System.out.println(orgdepartment.getDepartmentname()+"==="+orguser.getUsername()));
////
////
////
////            System.out.println(orguserList.size());
//        }
//
//
//
//
//
//
//
//        String fileName2 = "C:\\Users\\DTRL002\\Desktop\\book_novels\\book\\src\\main\\java\\cn\\book\\bus\\controller" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
//        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//        // 如果这里想使用03 则 传入excelType参数即可
//        EasyExcel.write(fileName2, Bclass.class)
//                .sheet("模板")
//                .doWrite(() -> {
//                    // 分页查询数据
//                    return arrayList;
//                });
//
//    }

    @GetMapping("banji")
    public void simple() {

        // 创建一个定长线程池，例如5个线程
        int threadCount = 40;
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
        final ArrayList<Bclass> arrayList = new ArrayList<>();
        //所有班级
        final List<Orgdepartment> list = orgdepartmentService.list();
        // 数据分页大小，每个线程处理多少数据
        int batchSize = (list.size() + threadCount - 1) / threadCount;
        // 将大列表拆分成多个小列表，然后提交到线程池处理
        for (int i = 0; i < list.size(); i += batchSize) {
            final int fromIndex = i;
            final int toIndex = Math.min(fromIndex + batchSize, list.size());
            Runnable worker = () -> processBatchbanji(list.subList(fromIndex, toIndex),arrayList);
            executorService.submit(worker);
        }
        // 关闭线程池，等待所有任务完成
        executorService.shutdown();
        try {
            // 阻塞直到所有任务都已完成
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("Interrupted while waiting for tasks to terminate.");
        }




        String fileName2 = "C:\\Users\\DTRL002\\Desktop\\book_novels\\book\\src\\main\\java\\cn\\book\\bus\\controller" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName2, Bclass.class)
                .sheet("模板")
                .doWrite(() -> {
                    // 分页查询数据
                    return arrayList;
                });

    }


    private  void processBatchbanji(List<Orgdepartment> batch,ArrayList<Bclass> arrayList) {
        for (Orgdepartment orgdepartment : batch) {
            System.out.println(orgdepartment.getDepartmentname());
            final LambdaQueryWrapper<Orguser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Orguser::getDepartmentid, orgdepartment.getId());
            //找学生
            final List<Orguser> orguserList = orguserService.list(wrapper);

            int count = 0;
            int yiping = 0;
            int weiping = 0;

            for (Orguser orguser : orguserList) {
                //总数
//                final LambdaQueryWrapper<BoEuStudentEavResult> countwrapper = new LambdaQueryWrapper<>();
//                countwrapper.eq(BoEuStudentEavResult::getStudentId, orguser.getUserid());
//                int i  = boEuStudentEavResultService.count(countwrapper);
//                count = count + i;
                System.out.println("学生班级==="+orgdepartment.getDepartmentname());

                //已评
                final LambdaQueryWrapper<BoEuStudentEavResult> yipingwrapper = new LambdaQueryWrapper<>();
                yipingwrapper.eq(BoEuStudentEavResult::getStudentId, orguser.getUserid()).eq(BoEuStudentEavResult::getTaskStatus,"已评教");
                int j  = boEuStudentEavResultService.count(yipingwrapper);
                yiping = yiping + j;

                //未评
                final LambdaQueryWrapper<BoEuStudentEavResult> weipingwrapper = new LambdaQueryWrapper<>();
                weipingwrapper.eq(BoEuStudentEavResult::getStudentId, orguser.getUserid()).eq(BoEuStudentEavResult::getTaskStatus,"未评教");
                int q  = boEuStudentEavResultService.count(weipingwrapper);
                weiping = weiping + q;
                //总数
                count= count+j+q;
                System.out.println(orguser.getUsername()+"==="+count);

            }

            final Bclass bclass = new Bclass();
            bclass.setId(orgdepartment.getId());
            bclass.setName(orgdepartment.getDepartmentname());
            bclass.setTatol(String.valueOf(count));
            bclass.setYiping(String.valueOf(yiping));
            bclass.setWeiping(String.valueOf(weiping));
            arrayList.add(bclass);


            System.out.println("111111111111111111111111111111111111111"+bclass+ new Date());
            System.out.println("count====="+count);
//
        }

    }



    @GetMapping("suili")
    public void simple1() {

        // 创建一个定长线程池，例如5个线程
        int threadCount = 40;
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
        final ArrayList<Bclass> arrayList = new ArrayList<>();
        //所有班级
        final List<WaterClass> list = waterClassService.list();
//        final List<Orgdepartment> list = orgdepartmentService.list();
        // 数据分页大小，每个线程处理多少数据
        int batchSize = (list.size() + threadCount - 1) / threadCount;
        // 将大列表拆分成多个小列表，然后提交到线程池处理
        for (int i = 0; i < list.size(); i += batchSize) {
            final int fromIndex = i;
            final int toIndex = Math.min(fromIndex + batchSize, list.size());
            Runnable worker = () -> processBatchbanji3(list.subList(fromIndex, toIndex),arrayList);
            executorService.submit(worker);
        }
        // 关闭线程池，等待所有任务完成
        executorService.shutdown();
        try {
            // 阻塞直到所有任务都已完成
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("Interrupted while waiting for tasks to terminate.");
        }










        String fileName2 = "C:\\Users\\DTRL002\\Desktop\\book_novels\\book\\src\\main\\java\\cn\\book\\bus\\controller" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName2, Bclass.class)
                .sheet("模板")
                .doWrite(() -> {
                    // 分页查询数据
                    return arrayList;
                });

    }




    private  void processBatchbanji3(List<WaterClass> batch,ArrayList<Bclass> arrayList) {
        for (WaterClass waterClass : batch) {
            System.out.println(waterClass.getName());

             LambdaQueryWrapper<Percla> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Percla::getClassId, waterClass.getId());

             List<Percla> perclaList = perclaService.list(wrapper);
            //总人数
            int count = perclaList.size();


            //已评人数
            int yiping = 0;
            //未评人数
            int weiping = 0;
            for (Percla percla : perclaList) {
                 LambdaQueryWrapper<Yi> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(Yi::getStudentId, percla.getStudentId());
                 Yi one = yiService.getOne(wrapper1);
                 if(null != one){
                     if (percla.getSum().equals(one.getSum())){
                         yiping = yiping + 1;
                     }else {
                         weiping = weiping + 1;
                     }
                 }else {
                     weiping = weiping + 1;
                 }


            }

            final Bclass bclass = new Bclass();
            bclass.setId(waterClass.getId());
            bclass.setName(waterClass.getName());
            bclass.setTatol(String.valueOf(count));
            bclass.setYiping(String.valueOf(yiping));
            bclass.setWeiping(String.valueOf(weiping));
            arrayList.add(bclass);
//
        }

    }


}

